﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace TileEngine
{
    public static class Camera
    {
        #region Fields
        private static Vector2 m_position = Vector2.Zero;
        private static Vector2 m_viewPortSize = Vector2.Zero;
        private static Rectangle m_worldRectangle = new Rectangle(0, 0, 0, 0);
        #endregion

        #region Properties
        public static Vector2 Position
        {
            get { return m_position; }
            set 
            {
                m_position = new Vector2(
                    MathHelper.Clamp(value.X,
                        m_worldRectangle.X,
                        m_worldRectangle.Width - ViewPortWidth),
                    MathHelper.Clamp(value.Y,
                        m_worldRectangle.Y,
                        m_worldRectangle.Height - ViewPortHeight)
                );
            }
        }

        public static Rectangle WorldRectangle
        {
            get { return m_worldRectangle; }
            set { m_worldRectangle = value; }
        }

        public static int ViewPortWidth
        {
            get { return (int)m_viewPortSize.X; }
            set { m_viewPortSize.X = value; }
        }

        public static int ViewPortHeight
        {
            get { return (int)m_viewPortSize.Y; }
            set { m_viewPortSize.Y = value; }
        }

        public static Rectangle ViewPort
        {
            get 
            {
                return new Rectangle(
                    (int)Position.X, (int)Position.Y,
                    ViewPortWidth, ViewPortHeight);
            }
        }
        #endregion

        #region Public Methods
        public static void Move(Vector2 a_offset)
        {
            Position += a_offset;
        }

        public static bool ObjectIsVisible(Rectangle a_bounds)
        {
            return (ViewPort.Intersects(a_bounds));
        }

        public static Vector2 WorldToScreen(Vector2 a_woldLocation)
        {
            return a_woldLocation - m_position;
        }

        public static Rectangle WorldToScreen(Rectangle a_worldRectangle)
        {
            return new Rectangle(
                a_worldRectangle.Left - (int)m_position.X,
                a_worldRectangle.Top - (int)m_position.Y,
                a_worldRectangle.Width,
                a_worldRectangle.Height);
        }

        public static Vector2 ScreenToWorld(Vector2 a_screenLocation)
        {
            return a_screenLocation + m_position;
        }

        public static Rectangle ScreenToWorld(Rectangle a_screenRectangle)
        {
            return new Rectangle(
                a_screenRectangle.Left + (int)m_position.X,
                a_screenRectangle.Top + (int)m_position.Y,
                a_screenRectangle.Width,
                a_screenRectangle.Height);
        }

        public static void Update(Vector2 a_pos)
        {
            Vector2 screenPos = Camera.WorldToScreen(a_pos);

            if (screenPos.X > (m_viewPortSize.X / 2))
            {
                Camera.Move(new Vector2(screenPos.X - (m_viewPortSize.X / 2), 0));
            }
            if (screenPos.X < (m_viewPortSize.X / 3.5f))
            {
                Camera.Move(new Vector2(screenPos.X - (m_viewPortSize.X / 3.5f), 0));
            }

            if (screenPos.Y > 250)
            {
                Camera.Move(new Vector2(0, screenPos.Y - 250));
            }
            
            //Fixa!
            if (screenPos.Y < 100)
            {
                Camera.Move(new Vector2(0, screenPos.Y - 100));
            }
        }
        #endregion
    }
}
